package com.dangdang.action.cart;

import com.dangdang.action.BaseAction;
import com.dangdang.service.CartService;
import com.dangdang.service.ServiceFactory;
import com.dangdang.util.Constants;

/**
 * 更新购物车中商品的数量
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-29
 */
public class UpdateCartAction extends BaseAction {

	private CartService cartService;

	/** 被更新的商品ID */
	private Integer productId;

	/** 更新的数量 */
	private int number;

	/**
	 *更改商品数量
	 * 
	 * @return
	 */
	public String execute() {
		cartService = ServiceFactory.getCartService(session);
		try {
			if (number >= 1) {
				cartService.updateNumber(productId, number);
			}
		} catch (Exception e) {
			// 用户输入出错了,造成的异常
			return Constants.FORWARD_SUCCESS;
		}
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 清空购物车
	 * 
	 * @return
	 */
	public String clearCart() {
		cartService = ServiceFactory.getCartService(session);
		cartService.clear();
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 删除所购买的商品
	 * 
	 * @return
	 */
	public String drop() {
		System.out.println("你删除了商品");
		cartService = ServiceFactory.getCartService(session);
		cartService.drop(productId);
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 恢复所删除的商品
	 * 
	 * @return
	 */
	public String recovery() {
		System.out.println("你恢复了商品");
		cartService = ServiceFactory.getCartService(session);
		cartService.recovery(productId);
		return Constants.FORWARD_SUCCESS;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}
}
